package com.glela.controller;

import com.alibaba.fastjson.JSONObject;
import com.glela.common.constant.CommonConstants;
import com.glela.common.util.DateUtil;
import com.glela.live.service.GetRedPackageService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.util.ParameterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jinweiwei on 2018/4/19.
 */
@RestController
@RequestMapping("/liveRedPackage")
public class GetRedPackageController extends LiveBaseController{

    @Autowired
    private GetRedPackageService getRedPackageService;

    @RequestMapping(value = "/getRedPackage",method = RequestMethod.POST,consumes = "application/json")
    public ResponseEntity<?> getRedPackage(@RequestBody JSONObject json, HttpServletRequest request){
        if (ParameterUtils.validParams(request.getHeader("userId"),json.getIntValue("redPackageId"))) {
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
        }
        Object object = getRedPackageService.getRedPackage(Integer.valueOf(request.getHeader("userId")),json.getIntValue("redPackageId"));
        if (object != null) {
            JSONObject jsonObject = JSONObject.parseObject(object.toString());
            return new ResponseEntity<>(new ResponseResultModel<>(jsonObject), HttpStatus.OK);
        }else {
            //已经取完了
            return new ResponseEntity<>(new ResponseResultModel<>(null), HttpStatus.OK);
        }
    }

    @RequestMapping(value = "/getRedPackageHistory",method = RequestMethod.POST,consumes = "application/json")
    public ResponseEntity<?> getRedPackageHistory(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        if (ParameterUtils.validParams(request.getHeader("userId"),jsonObject.getIntValue("redPackageId"))) {
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
        }
        List<String> list = redisUtil.lRang(CommonConstants.hongBaoConsumedList+jsonObject.getIntValue("redPackageId"),0,-1);
        if (list!=null && !list.isEmpty()){
            List<JSONObject> results = new ArrayList<>();
            for (String s: list) {
                JSONObject json = JSONObject.parseObject(s);
                String dateStr = DateUtil.longToMillString(json.getLongValue("time"));
                json.put("time",dateStr);
                results.add(json);
//                System.out.println("======红包金额："+json.getDouble("money")+"===========用户id："+json.getString("userId")+"======领取时间："+ DateUtil.longToMillString(json.getLongValue("time")));
            }
            return new ResponseEntity<>(new ResponseResultModel<>(results), HttpStatus.OK);
        }
        return new ResponseEntity<>(new ResponseResultModel<>(list), HttpStatus.OK);
    }

    @RequestMapping(value = "/testGetRedPackage",method = RequestMethod.POST,consumes = "application/json")
    public String testGetRedPackage(){
        for (int i = 0; i < 10; i++) {
            Object object = getRedPackageService.getRedPackage(20001+i,2);
        }
        return null;
    }

    @RequestMapping(value = "/consumerRedPackage",method = RequestMethod.POST,consumes = "application/json")
    public String consumeRedPackage(@RequestBody JSONObject jsonObject){

        for (int i = 0; i < 9999; i++) {
            Object object = redisUtil.eval(CommonConstants.tryGetHongBaoScript, 6, CommonConstants.hongBaoList+2, CommonConstants.hongBaoConsumedList+2, CommonConstants.hongBaoConsumedMap+2, "userId",CommonConstants.hongBaoConsumedUpdateList,System.currentTimeMillis()+"");
        }
        return "over";
    }
}
